package com.xindacloud.sms.portal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.portal.dao.SmsDeliveryReceiptMapper;
import com.xindacloud.sms.portal.dto.SmsReportDTO;
import com.xindacloud.sms.portal.entity.SmsDeliveryReceipt;
import com.xindacloud.sms.portal.entity.SmsPricing;
import com.xindacloud.sms.portal.service.ISmsDeliveryReceiptService;
import com.xindacloud.sms.portal.service.ISmsPricingService;
import com.xindacloud.sms.portal.vo.QuerySmsDeliveryReceiptVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SmsDeliveryReceiptServiceImpl extends ServiceImpl<SmsDeliveryReceiptMapper, SmsDeliveryReceipt> implements ISmsDeliveryReceiptService {

    final SmsDeliveryReceiptMapper deliveryReceiptMapper;

    final ISmsPricingService pricingService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public SmsDeliveryReceiptServiceImpl(SmsDeliveryReceiptMapper deliveryReceiptMapper, ISmsPricingService pricingService) {
        this.deliveryReceiptMapper = deliveryReceiptMapper;
        this.pricingService = pricingService;
    }

    @Override
    public List<QuerySmsDeliveryReceiptVO> queryDeliveryReceiptFor(SmsReportDTO smsReportDTO) {

        final List<QuerySmsDeliveryReceiptVO> querySmsDeliveryReceiptVOList = deliveryReceiptMapper.queryDeliveryReceipt(smsReportDTO);
        return querySmsDeliveryReceiptVOList.stream()
                .peek(rs -> {

                    rs.setCountryName(getSmsPricing(rs.getNetwork()).isPresent()
                            ? getSmsPricing(rs.getNetwork()).get().getCountryName()
                            : "Unknown");
                    rs.setNetworkName(getSmsPricing(rs.getNetwork()).isPresent()
                            ? getSmsPricing(rs.getNetwork()).get().getNetworkName()
                            : "Unknown");
                })
                .collect(Collectors.toList());
    }

    private Optional<SmsPricing> getSmsPricing(String network) {
        if (StringUtils.isEmpty(network)) {
            return Optional.empty();
        }
        final Map<String, SmsPricing> smsPricingMap = getStringSmsPricingMap();
        if (CollectionUtils.isEmpty(smsPricingMap)) {
            return Optional.empty();
        }
        final SmsPricing smsPricing = smsPricingMap.get(network);
        if (Objects.isNull(smsPricing)) {
            return Optional.empty();
        }
        return Optional.of(smsPricing);
    }

    private Map<String, SmsPricing> getStringSmsPricingMap() {
        return pricingService.queryPricingGroupByCountryCodeAndNetworkCode()
                .stream()
                .filter(smsPricing -> StringUtils.isNotEmpty(smsPricing.getNetworkCode()))
                .collect(Collectors.toMap(SmsPricing::getNetworkCode, Function.identity()));
    }
}
